﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using RainEngine.SceneManagement;
using RainEngine.Controllers;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 竖直滑动条
    /// </summary>
    public class UIVSlider:UISliderBase  
    {
        /// <summary>
        /// 创建一个默认竖直滑动条
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        public UIVSlider(RainGE engine, Scene setScene)
            : base(engine, setScene, Vector2.Zero, "UIDxutcontrols", DefaultSliderRailSize, DefaultSliderRailRect, DefaultSliderButtonSize, DefaultSliderButtonRect, DefaultSliderButtonRect, DefaultSliderButtonRect,true)
        {
            //设置竖直滑动条滑块位置
            this.sliderButtonPosition = new Vector2(0, - sliderButtonSize.X / 2);
        }

        public override void Update(GameTime gameTime)
        {

            //如果鼠标在滑动条上且左键按下  
            if (isMouseInside && Input.MouseLeftButtonPressed)
            {
                isSliding = true;

                float value = 0;
                //计算滑动条的值
                value = minValue + ((int)(System.Math.Round((Input.MousePos.Y - Position.Y) * tickFrequency / sliderRailSize.X)) * (maxValue - minValue) / tickFrequency);
                //不让value值超出范围
                if (value < minValue)
                    value = minValue;
                else if (value > maxValue)
                    value = maxValue;

                //如果值变化则重新设置新值并引发ValueChanged事件
                if (this.value != value)
                {
                    this.value = value;
                    OnValueChanged();
                }

                else
                    isSliding = false;
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// 绘制滑动条
        /// </summary>
        public override int Draw(GameTime gameTime)
        {

            //获取图像淡入淡出的透明颜色
            alphaTextureColor = new Color(Color.R,Color.G,Color.B, Scene.TransitionAlpha);

            //绘制滑动条轨道轨道，竖直滑动条的轨道需要顺时针旋转90度
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)(Position.X + size.X / 2 + sliderRailSize.Y / 2), (int)Position.Y, (int)sliderRailSize.X, (int)sliderRailSize.Y), sliderRailRect, alphaTextureColor, MathHelper.PiOver2, Origin, SpriteEffect, LayerDepth);

            //根据鼠标移入还是按下设置滑块图像源矩阵
            Rectangle buttonRect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed && isMouseInside) ? sliderButtonRectClicked : sliderButtonRectHighlight) : sliderButtonRectOrigin;
            //绘制滑块，竖直滑动条的滑块需要顺时针旋转90度
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)(Position.X + size.X), (int)sliderButtonPosition.Y, (int)sliderButtonSize.X, (int)sliderButtonSize.Y), buttonRect, alphaTextureColor, MathHelper.PiOver2, Origin, SpriteEffect, LayerDepth);
            return 1;

        }

        #region 事件相关处理程序

        /// <summary>
        /// 重置竖直轨道上滑块的位置
        /// </summary>
        protected override  void  ResetSliderButtonPosition()
        {
            //根据value值重置按钮的位置
            sliderButtonPosition.Y = (value - minValue) * (sliderRailSize.X) / (maxValue - minValue) + Position.Y - sliderButtonSize.X / 2;

            //不让按钮超出滑动条边界
            if (sliderButtonPosition.Y < Position.Y - sliderButtonSize.X / 2)
                sliderButtonPosition.Y = Position.Y - sliderButtonSize.X / 2;
            else if (sliderButtonPosition.Y > Position.Y + sliderRailSize.X - sliderButtonSize.X / 2)
                sliderButtonPosition.Y = Position.Y + sliderRailSize.X - sliderButtonSize.X / 2;
        }
        
        /// <summary>
        /// 设置竖直滑动条大小，竖直滑动条的宽为滑块的高，高为滑动条轨道的长
        /// </summary>
        protected override void ResetSize()
        {
            this.size = new Vector2(sliderButtonSize.Y, sliderRailSize.X);
        }

        #endregion
    }
}
